/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/// \file engineCore.h
/// \brief Contains classes and functions pertinant to the core of the engine


#ifndef ENGINECORE_H
#define ENGINECORE_H
#define VERSION __TIMESTAMP__

/// DEBUGING METHOD TO CHECK IF HEAP IS OK
#define HEAPCHK { if( _heapchk() != _HEAPOK ) { cout << "Heap Corrupted: " << __LINE__ << endl; } }

#define DLLEXPORT extern "C" __declspec(dllexport)
#define DLLIMPORT __declspec(dllimport)

#undef CLASSDECL
#ifdef ENGINECORE
	#define CLASSDECL __declspec(dllexport)
#else
	#pragma comment( lib, "EngineCore.lib" )
	#define CLASSDECL __declspec(dllimport)
#endif

#if (defined(__unix__) || defined(__linux__) || defined(__FreeBSD__) || defined(__sun))
	#define WINDOWS 0
	#define UNIX 1
#else
	#define WINDOWS 1
	#define UNIX 0
#ifdef UNIX
#undef UNIX
#endif

#endif
#include "ModuleBase.h" // Only dependency is the module base the core will be running.
#include <iostream>
#include <list>

//
// The engine core is the base of the entire engine where modules can be
// loaded and initalized and eventually run.  It will cotain the loop for
// which the engine will run.
//


/// This namespace contains all the classes and functions to run Modular Engine

/// Any class or function that is part of the core to Modular Engine will be found in this
/// namespace
namespace ModularEngine
{

	/// The all important Functor to be used throughout various modules
	class CLASSDECL Functor
	{
	public:
		virtual void operator() () = 0;
	};

	/// A Soon to be deprecated functor for Mouse or Keyboard Input
	class CLASSDECL KMFunctor
	{
	public:
		virtual void operator() (unsigned int nKey) = 0;
	};

	/// The main class from which all other modules and loaded and run. 

	/// This class stores all the pointers to the various modules, initializes 
	/// them, runs them, and eventually closes them at the end of the program.
	class CLASSDECL EngineCore: public Singleton<EngineCore>
	{
	protected:
		/// If it's not running, close everything.
		bool mbRunning; 
		/// Is it initialized properly?
		bool mbInitialized;	
		/// list of modules loaded in the engine
		std::list<ModuleBase *> mlModuleList; 

	public:
		/// Initialize all the modules that have been loaded.
		int init();
		/// Run the engine until something tells it to stop();
		int run(); 
		/// set mbRunning to false
		int stop(); 
		/// returns mbRunning
		bool status(); 

		/// default error showing utility
		void throwError( char *errorText, char *fileName = __FILE__, int lineNumber = __LINE__ ); 

		/// load a module into the engine.
		int loadModule( ModuleBase *module );

		EngineCore();
		~EngineCore();

	};

}


#endif
